package com.yanqu.road.dao.impl.activity.douluodalu;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.douluodalu.log.DouLuoDaLuEventDefendData;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DouLuoDaLuEventDefendDataDaoImpl extends TempDataObjectDao<DouLuoDaLuEventDefendData> {


    public static String getDouLuoDaLuEventDefendDataSql = "select * from `t_u_douluodalu_event_defend` where `activityId`=? and `userId` = ? order by `logTime` desc limit 50";
    public List<DouLuoDaLuEventDefendData> getDouLuoDaLuEventDefendDataList(int activityId, long userId) {
        List<DouLuoDaLuEventDefendData> result = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getDouLuoDaLuEventDefendDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                result.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertDouLuoDaLuEventDefendDataSql = "insert into `t_u_douluodalu_event_defend`(activityId,userId,enemyUserId,enemyBaseInfo,damage,blood,type,lose,logTime," +
            "eventType,rewards,consume,monsterId,eventId,paramList,attackType,friendBaseInfo) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(DouLuoDaLuEventDefendData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getEnemyUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getEnemyBaseInfo() == null ? "" :JSON.toJSONString(data.getEnemyBaseInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getDamage()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getBlood()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getType()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getLose()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLogTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEventType()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getRewards()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getConsume()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getMonsterId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getEventId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getParamList())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getAttackType()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getFriendBaseInfo() == null ? "" : JSON.toJSONString(data.getFriendBaseInfo())));
        if (getDb().executeNoneQuery(insertDouLuoDaLuEventDefendDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateDouLuoDaLuEventDefendDataSql = "update `t_u_douluodalu_event_defend` set userId=?,enemyUserId=?,enemyBaseInfo=?,damage=?,blood=?,type=?,lose=?,logTime=? where activityId=?";

    @Override
    protected boolean updateData(DouLuoDaLuEventDefendData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getEnemyUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getEnemyBaseInfo() == null ? "" : JSON.toJSONString(data.getEnemyBaseInfo())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getDamage()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getBlood()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getType()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getLose()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLogTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        if (getDb().executeNoneQuery(updateDouLuoDaLuEventDefendDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected DouLuoDaLuEventDefendData parseData(ResultSet rs) throws SQLException {
        DouLuoDaLuEventDefendData data = new DouLuoDaLuEventDefendData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setEnemyUserId(rs.getLong("enemyUserId"));
        String str = rs.getString("enemyBaseInfo");
        if(!StringUtils.isNullOrEmpty(str)){
            data.setEnemyBaseInfo(JSON.parseObject(str, UserBaseInfo.class));
        }
        str = rs.getString("friendBaseInfo");
        if(!StringUtils.isNullOrEmpty(str) && !"{}".equals(str)){
            data.setFriendBaseInfo(JSON.parseObject(str, UserBaseInfo.class));
        }
        data.setAttackType(rs.getInt("attackType"));
        data.setDamage(rs.getLong("damage"));
        data.setBlood(rs.getLong("blood"));
        data.setType(rs.getInt("type"));
        data.setLose(rs.getString("lose"));
        data.setLogTime(rs.getLong("logTime"));
        data.setEventType(rs.getInt("eventType"));
        data.setRewards(rs.getString("rewards"));
        data.setConsume(rs.getString("consume"));
        data.setMonsterId(rs.getInt("monsterId"));
        data.setEventId(rs.getInt("eventId"));
        str = rs.getString("paramList");
        if(!StringUtils.isNullOrEmpty(str)){
            data.setParamList(JSON.parseObject(str, new TypeReference<List<String>>() {}));
        }
        return data;
    }
}
